﻿using System;
using System.Collections.Generic;
using System.Text;
using EventBroker;
using System.Windows.Forms;


namespace TestEventBroker
{
    class A
    {
        public const string m_id = "Class A";

        [CEventSubscription("m_strTopic:Event1", Thread = EThreadOption.Background)]
        public void OnHandler1(object sender, MyEventArgs e)
        {
            MessageBox.Show("I am in OnHandler1 and MyEventArgs is {0}", e.m_id);
        }

        [CEventSubscription("m_strTopic:Event2", Thread = EThreadOption.Background)]
        public void OnHandler2(object sender, MyEventArgs e)
        {
            MessageBox.Show("I am in OnHandler2 and MyEventArgs is {0}", e.m_id);
        }

        [CEventSubscription("m_strTopic:Event1", Thread = EThreadOption.Background)]
        [CEventSubscription("m_strTopic:Event2", Thread = EThreadOption.Background)]
        public void OnHandler3(object sender, MyEventArgs e)
        {
            MessageBox.Show("I am in OnHandler3 and MyEventArgs is {0}", e.m_id);
        }
    }

    class B
    {
        [CEventPublication("m_strTopic:Event1")]
        public event EventHandler<MyEventArgs> Event1;

        [CEventPublication("m_strTopic:Event2")]
        public event EventHandler<MyEventArgs> Event2;

        public void FireEvent1(MyEventArgs e)
        {
            MessageBox.Show("Going to fire Event1");
            if (Event1 != null)
            {
                Event1(this, e);
            }
        }
        public void FireEvent2(MyEventArgs e)
        {
            MessageBox.Show("Going to fire Event2");
            if (Event2 != null)
            {
                Event2(this, e);
            }
        }
    }
    public class MyEventArgs : EventArgs
    {
        public string m_id;
    }
    public class Driver
    {
        public static void Test()
        {
            B b = new B();
            A a = new A();

            CEventManager.Register(b);
            CEventManager.Register(a);

            MyEventArgs e1 = new MyEventArgs();
            MyEventArgs e2 = new MyEventArgs();
            e1.m_id = "Event sArgs for event 1";
            e2.m_id = "Event sArgs for event 2";
            b.FireEvent1(e1);
            b.FireEvent2(e2);

            CEventManager.Unregister(b);
            CEventManager.Unregister(a);
        }
    }

    /////////////////////////
    //THIS IS THE ACTUAL CODE
    /////////////////////////

    ////Step 1 Create delegate object
    //public delegate void MyHandler1(object objSender_p, MyEventArgs eventArgs_p);
    //public delegate void MyHandler2(object objSender_p, MyEventArgs eventArgs_p);
    ////Step 2 Create event dgEventTopicFire methods
    //class A
    //{
    //    public const string m_id = "Class A";
    //    public void OnHandler1(object objSender_p, MyEventArgs eventArgs_p)
    //    {
    //        Console.WriteLine("I am in OnHandler1 and MyEventArgs is {0}", eventArgs_p.m_id);
    //    }
    //    public void OnHandler2(object objSender_p, MyEventArgs eventArgs_p)
    //    {
    //        Console.WriteLine("I am in OnHandler2 and MyEventArgs is {0}", eventArgs_p.m_id);
    //    }
    //    //Step 3 create delegates, plug in the dgEventTopicFire and register with the object that will fire the events
    //    public A(B b)
    //    {
    //        MyHandler1 d1 = new MyHandler1(OnHandler1);
    //        MyHandler2 d2 = new MyHandler2(OnHandler2);
    //        b.Event1 += d1;
    //        b.Event2 += d2;
    //    }
    //}
    ////Step 4 Calls the encapsulated methods through the delegates (fires events)
    //class B
    //{
    //    public event MyHandler1 Event1;
    //    public event MyHandler2 Event2;
    //    public void FireEvent1(MyEventArgs eventArgs_p)
    //    {
    //        if (Event1 != null)
    //        {
    //            Event1(this, eventArgs_p);
    //        }
    //    }
    //    public void FireEvent2(MyEventArgs eventArgs_p)
    //    {
    //        if (Event2 != null)
    //        {
    //            Event2(this, eventArgs_p);
    //        }
    //    }
    //}
    //public class MyEventArgs : m_eventArgs
    //{
    //    public string m_id;
    //}
    //public class Driver
    //{
    //    public static void Main()
    //    {
    //        B b = new B();
    //        A a = new A(b);
    //        MyEventArgs e1 = new MyEventArgs();
    //        MyEventArgs e2 = new MyEventArgs();
    //        e1.m_id = "Event sArgs for event 1";
    //        e2.m_id = "Event sArgs for event 2";
    //        b.FireEvent1(e1);
    //        b.FireEvent2(e2);
    //    }
    //}
}
